﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace WindowsGame4
{
    class Explosion_DestroyState : State
    {
        private static Explosion_DestroyState instance;

        private Explosion_DestroyState()
        {
            base.Name = "ActivateState";
        }

        public override void enter(AIObject aiObject)
        {
            
            AIExplosion aiExplosion = (AIExplosion)aiObject;
            Explosion explosion = (Explosion)aiExplosion.Explosion;
            GameInfo gameInfo = GameInfo.getInstance();

            explosion.Visible = true;
            EventEmitter.getInstance().registerMessage(15000000, explosion.Id, explosion.Id, Message.MessageType.msg_destroy);

           
            // Check colisions
            ArrayList type = new ArrayList();
            type.Add("colision");
            type.Add("player");
            type.Add("enemy");
            ArrayList ignore = new ArrayList();


            Dictionary<String, Object> colisions = gameInfo.getObjects("colision");

            foreach (KeyValuePair<String, Object> i in colisions)
            {
                Colision colision = (Colision)i.Value;
                if (colision.Life == 0)
                    ignore.Add(colision.Id);
            }

            Object victim = explosion.checkColision(type, ignore);
            if (victim != null)
            {
                if (victim.Type == Object.TypeObject.colision)
                {
                    gameInfo.receivedAttack(victim, 20);
                }
                else
                {
                    Object emisor = gameInfo.getObject(explosion.IdEmissor);

                    if (emisor != null && (emisor.Team != victim.Team || emisor.Id == victim.Id))
                    {
                        if (victim.Blinking == false)
                        {
                            EventEmitter.getInstance().registerMessage(0, victim.Id, victim.Id, Message.MessageType.msg_attacked);

                            if (emisor.Id != victim.Id)
                                emisor.Kills = emisor.Kills + 1;
                        }
                        //explosion.changeAnimation("effect");
                    }      
                }
            }
        }

        public static Explosion_DestroyState getInstance()
        {
            if (instance == null)
                instance = new Explosion_DestroyState();
            return instance;
        }

        public override void execute(AIObject aiObject)
        {
            AIExplosion aiExplosion = (AIExplosion)aiObject;
            Explosion explosion = (Explosion)aiExplosion.Explosion;
           
        }

        public override void exit(AIObject aiObject)
        {
            //Console.WriteLine("<Bomb_ActivateState> exit");
        }

        public override Boolean onMessage(AIObject aiObject, Message message)
        {
            //Console.WriteLine("<Explosion_DestroyState> onMessage " + message.Type);
            Boolean correct = false;
            switch (message.Type)
            {
                case Message.MessageType.msg_destroy:
                    {
                        //Console.ReadKey();
                        GameInfo gameInfo = GameInfo.getInstance();
                        gameInfo.removeObject(aiObject.Id, Object.TypeObject.explosion);
                        correct = true;
                        break;
                    }
            }
            return correct;
        }
    }
}
